package ONLINE_EXAM;

public class btd {
    public static void main(String[] args) {

    }
    //默认从小到大排序
    public static int BinarySearch(int[] nums,int target,int left,int right){
        if (left>right){
            return -1;
        }
        int mid = (left+right)/2;
        if (nums[mid] > target){
            return BinarySearch(nums,target,left,mid-1);//leftside
        }else if (nums[mid] < target){
            return BinarySearch(nums,target,mid,right);//rightSide
        }else {
            return mid;
        }
    }

    public int BinarySearch1(int[] nums, int target) {
        int mid, left = 0, right = nums.length - 1;

        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (target < nums[mid]){
                right = mid - 1;//right
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

}
